require("../../assets/css/normalize.css");
require("../../assets/css/reset.css");
require("../../assets/fonts/iconfont.css");
require("./register.less");

// 功能一:验证码实现
// 引入模块
const captcha = require("captcha-mini");
// 引入axios
const axios = require("axios");
// 引入工具js

const dom = require("../../utils/dom.js");
// 初始化
let captcha1 = new captcha();
let code1 = "";
captcha1.draw(document.querySelector("#captcha1"), r => {
	code1 = r.toLowerCase();
	console.log(code1);
});
// 功能二:表单验证

dom.get("#regBtn").onclick = function () {
	let error = dom.get("#error");

	// 手机号验证
	let tel = dom.get("#tel").value;
	let regxp = /^1[3456789]\d{9}$/i;
	if (regxp.test(tel)) {
		error.textContent = "";
	} else {
		error.textContent = "请输入正确的电话号码";
		return;
	}
	// 验证码验证
	let code = dom.get("#code").value;
	if (code === code1) {
		error.textContent = "";
	} else {
		error.textContent = "验证码输入错误";
		return;
	}
	// 密码验证
	let pwd = dom.get("#pwd").value;
	if (pwd.length >= 6) {
		error.textContent = "";
	} else {
		error.textContent = "请输入6位数以上密码";
		return;
	}
	// 再次确认密码验证
	let pwd2 = dom.get("#pwd2").value;
	if (pwd === pwd2) {
		error.textContent = "";
	} else {
		error.textContent = "请重新核对后输入";
		return;
	}
	// 都通过了验证,再发送请求
	axios
		.post("http://fitness.h5.itsource.cn:3701/api/user/register", {
			account: tel,
			password: pwd,
		})
		.then(res => {
			// console.log(res);
			if (res.data.errno === 0) {
				location.href = "login.html";
				alert("恭喜,注册成功!");
			} else {
				alert("很抱歉,注册失败!");
			}
		});
};
